<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24"> </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <a-tabs v-model="activeKey" @change="handleChangeTab">
      <a-tab-pane key="notGenerated" tab="未生成">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="loadNotGeneratedData">
            <a-row :gutter="24">
              <a-col :span="24" style="margin-top: 10px">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button
                    v-has="'org.jeecg.modules:tsl_pro_workshop_plan:add'"
                    @click="addWorkshopPlan"
                    type="primary"
                    icon="plus"
                    >添加</a-button
                  >
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
          <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
          >项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          <span style="float: right">
            <a-popover title="自定义列" trigger="click" placement="leftBottom">
              <template slot="content">
                <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                  <a-row style="width: 400px">
                    <template v-for="(item, index) in defColumns">
                      <template v-if="item.key !== 'rowIndex' && item.dataIndex !== 'action'">
                        <a-col :span="12"
                          ><a-checkbox :value="item.dataIndex"
                            ><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox
                        ></a-col>
                      </template>
                    </template>
                  </a-row>
                </a-checkbox-group>
              </template>
              <a><a-icon type="setting" />设置</a>
            </a-popover>
          </span>
        </div>
        <!-- table区域-begin -->
        <div>
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            class="j-table-force-nowrap"
            :scroll="{ x: true }"
            :columns="planColumns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            :customRow="rnoSelect"
          >
          </a-table>
          <a-divider>产品信息</a-divider>
          <j-vxe-table
            keep-source
            :ref="refKeys[1]"
            :loading="unMakeTable.loading"
            :columns="unMakeTable.columns"
            :dataSource="unMakeTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="false"
            :toolbar="false"
          />
          <a-divider>包材信息</a-divider>
          <j-vxe-table
            keep-source
            :ref="refKeys[0]"
            :loading="packageTable.loading"
            :columns="packageTable.columns"
            :dataSource="packageTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="false"
            :toolbar="false"
          />
        </div>
      </a-tab-pane>
      <a-tab-pane key="tempSave" tab="暂存中">
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
          <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
          >项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          <span style="float: right">
            <a-popover title="自定义列" trigger="click" placement="leftBottom">
              <template slot="content">
                <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                  <a-row style="width: 400px">
                    <template v-for="(item, index) in defColumns">
                      <template v-if="item.key !== 'rowIndex' && item.dataIndex !== 'action'">
                        <a-col :span="12"
                          ><a-checkbox :value="item.dataIndex"
                            ><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox
                        ></a-col>
                      </template>
                    </template>
                  </a-row>
                </a-checkbox-group>
              </template>
              <a><a-icon type="setting" />设置</a>
            </a-popover>
          </span>
        </div>
        <a-table
          ref="tempSaveTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="tempSaveTable.dataSource"
          :pagination="{
            ...tempSaveTable.ipagination,
            onChange: handleTempSavePageChange, // 监听页码变化
            onShowSizeChange: handleTempSavePageSizeChange, // 监听每页数量变化
          }"
          :loading="tempSaveTable.loading"
          :rowSelection="{
            selectedRowKeys: tempSaveTable.selectedRowKeys,
            onChange: onWorkshopPlanSelectChangeTempSave,
            type: 'radio',
          }"
          :customRow="rowSelect"
        >
          <span slot="action" slot-scope="text, record">
            <a v-has="'org.jeecg.modules:tsl_pro_workshop_plan:edit'" @click="handleEdit(record)">编辑</a>
            <a-divider v-has="'org.jeecg.modules:tsl_pro_workshop_plan:edit'" type="vertical" />
            <a @click="handleApproval(record)">提交申请</a>
            <a-divider type="vertical" />
            <a-dropdown>
              <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a @click="handleDetail(record)">详情</a>
                </a-menu-item>
                <a-menu-item v-has="'org.jeecg.modules:tsl_pro_workshop_plan:delete'">
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </a-table>
        <j-vxe-table
          keep-source
          :ref="refKeys[2]"
          :loading="detailTable.loading"
          :columns="detailTable.columns"
          :dataSource="detailTable.dataSource"
          :maxHeight="300"
          :rowNumber="true"
          :rowSelection="false"
          :toolbar="false"
        />
      </a-tab-pane>
      <a-tab-pane key="approved" tab="已审批">
        <div
          class="table-page-search-wrapper"
          style="display: flex; align-items: center; padding-top: 10px; padding-bottom: 24px; gap: 16px"
        >
          <a-form layout="inline" style="width: 350px">
            <a-form-model-item label="生产车间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workshopCode">
              <j-dict-select-tag
                type="list"
                v-model="model.workshopCode"
                dictCode="erp_production_execution_organization,depart_name,depart_code"
                placeholder="请选择所属车间"
              />
            </a-form-model-item>
          </a-form>
          <a-form-item label="制单日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-date
              placeholder="请选择开始日期"
              class="query-group-cust"
              v-model="queryParamApproved.createTime_begin"
            ></j-date>
            <span class="query-group-split-cust"></span>
            <j-date
              placeholder="请选择结束日期"
              class="query-group-cust"
              v-model="queryParamApproved.createTime_end"
            ></j-date>
          </a-form-item>
          <a-button type="primary" @click="loadApprovedData" icon="search" style="margin-top: -3px">查询</a-button>
        </div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
          <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
          >项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
          <span style="float: right">
            <a-popover title="自定义列" trigger="click" placement="leftBottom">
              <template slot="content">
                <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                  <a-row style="width: 400px">
                    <template v-for="(item, index) in defColumns">
                      <template v-if="item.key !== 'rowIndex' && item.dataIndex !== 'action'">
                        <a-col :span="12"
                          ><a-checkbox :value="item.dataIndex"
                            ><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox
                        ></a-col>
                      </template>
                    </template>
                  </a-row>
                </a-checkbox-group>
              </template>
              <a><a-icon type="setting" />设置</a>
            </a-popover>
          </span>
        </div>
        <a-table
          ref="approvedTable"
          size="middle"
          bordered
          rowKey="id"
          class="j-table-force-nowrap"
          :scroll="{ x: true }"
          :columns="columns"
          :dataSource="approvedTable.dataSource"
          :pagination="{
            ...approvedTable.ipagination,
            onChange: handleApprovedPageChange, // 监听页码变化
            onShowSizeChange: handleApprovedPageSizeChange, // 监听每页数量变化
          }"
          :loading="approvedTable.loading"
          :rowSelection="{
            selectedRowKeys: approvedTable.selectedRowKeys,
            onChange: onWorkshopPlanSelectChangeApproved,
            type: 'radio',
          }"
          :customRow="rowSelect"
        >
          <span slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">变更</a>
            <a-divider type="vertical" />
            <a @click="handleDetail(record)">详情</a>
            <a-divider type="vertical" />
            <a @click="handleApproval(record)">撤回</a>
            <a-divider type="vertical" />
            <a v-if="record.isClose == 1" @click="OpenOrClose(record)">开启</a>
            <a v-if="record.isClose != 1" @click="OpenOrClose(record)">关闭</a>
          </span>
        </a-table>
        <j-vxe-table
          keep-source
          :ref="refKeys[3]"
          :loading="detailTable.loading"
          :columns="detailTable.columns"
          :dataSource="detailTable.dataSource"
          :maxHeight="300"
          :rowNumber="true"
          :rowSelection="false"
          :toolbar="false"
        />
      </a-tab-pane>
    </a-tabs>
    <tsl-pro-WorkshopPlan-modal ref="modalForm" @ok="modalFormOk" />
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import TslProWorkshopPlanModal from './modules/TslProWorkshopPlanModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import '@/assets/less/TableExpand.less'
import ActApplyBtn from '@views/flowable/components/ActApplyBtn.vue'
import { getAction } from '@api/manage'
import ActHistoricDetailBtn from '@views/flowable/components/ActHistoricDetailBtn.vue'
import { deepClone, getDate } from '../../utils'
import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
import { JVXETypes } from '@/components/jeecg/JVxeTable'
import { postAction } from '@api/manage'

export default {
  name: 'TslProWorkshopPlanList',
  mixins: [JeecgListMixin, JVxeTableModelMixin],
  components: {
    TslProWorkshopPlanModal,
    ActApplyBtn,
    ActHistoricDetailBtn,
  },
  data() {
    return {
      refKeys: ['unDetail', 'noMakeDetail', 'tempSaveDetail', 'approvedDetail'],
      unMakeTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '调度单号',
            key: 'scheduleCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 150,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '订单编号',
            key: 'orderCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 150,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '客户物料编码',
            key: 'customCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 150,
          },
          {
            title: '物料编码',
            key: 'productCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 150,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '物料名称',
            key: 'productBatchNo',
            type: JVXETypes.normal,
            align: 'center',
            width: 300,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '规格型号',
            key: 'materialSpec',
            dataIndex: 'materialSpec',
            type: JVXETypes.normal,
            align: 'center',
            width: 100,
          },
          {
            title: '配方号',
            key: 'formulaNumber',
            dataIndex: 'formulaNumber',
            align: 'center',
            width: 150,
          },
          {
            title: '主单位',
            key: 'meUnit',
            dataIndex: 'meUnit',
            dictCode: 'ji_unit',
            type: JVXETypes.select,
            align: 'center',
            width: 100,
            disabled: true,
          },
          {
            title: '主数量',
            key: 'mainQuantity',
            dataIndex: 'mainQuantity',
            align: 'center',
            width: 120,
          },
          {
            title: '辅单位',
            key: 'auUnit',
            dataIndex: 'auUnit',
            dictCode: 'zl_unit',
            type: JVXETypes.select,
            align: 'center',
            width: 100,
            disabled: true,
          },
          {
            title: '辅数量',
            key: 'auxiliaryQuantity',
            dataIndex: 'auxiliaryQuantity',
            align: 'center',
            width: 120,
          },
          // {
          //   title: '库存',
          //   key: 'stockQuantity',
          //   dataIndex: 'stockQuantity',
          //   width: 120,
          // },
          {
            title: '生产数量',
            key: 'planQuantity',
            dataIndex: 'planQuantity',
            align: 'center',
            width: 120,
          },
          {
            title: '特殊说明',
            key: 'specialRemark',
            dataIndex: 'specialRemark',
            align: 'center',
            width: 300,
          },
          // {
          //   title: '配方号',
          //   key: 'formulaNumber',
          //   dataIndex: 'formulaNumber',
          //   width: 100,
          //   disabled: true,
          // },
          // {
          //   title: '配方编码',
          //   key: 'formulaCode',
          //   dataIndex: 'formulaCode',
          //   width: 180,
          // },
          // {
          //   title: '包装要求',
          //   key: 'packReq',
          //   dataIndex: 'packReq',
          //   width: 300,
          // },
        ],
      },
      packageTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '物料编码',
            key: 'packageCode',
            align: 'center',
            minWidth: 120,
          },
          {
            title: '物料名称',
            key: 'packageName',
            align: 'center',
            minWidth: 300,
          },
          {
            title: '规格型号',
            key: 'noneSpec',
            dataIndex: 'noneSpec',
            align: 'center',
            minWidth: 150,
          },
          {
            title: '包材类别',
            key: 'packageInOutType',
            dataIndex: 'packageInOutType',
            align: 'center',
            minWidth: 150,
          },
          {
            title: '比例数量',
            key: 'proportion',
            dataIndex: 'proportion',
            align: 'center',
            minWidth: 120,
          },
          {
            title: '包装要求',
            key: 'requirements',
            dataIndex: 'requirements',
            align: 'center',
            minWidth: 300,
          },
        ],
      },
      detailTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '调度单号',
            key: 'scheduleCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 200,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '订单编号',
            key: 'orderCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 150,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '客户物料编码',
            key: 'customCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 150,
          },
          {
            title: '物料编码',
            key: 'productCode',
            type: JVXETypes.normal,
            align: 'center',
            width: 150,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '物料名称',
            key: 'productBatchNo',
            type: JVXETypes.normal,
            align: 'center',
            width: 300,
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '规格型号',
            key: 'materialSpec',
            dataIndex: 'materialSpec',
            type: JVXETypes.normal,
            align: 'center',
            width: 100,
          },
          {
            title: '配方号',
            key: 'formulaNumber',
            dataIndex: 'formulaNumber',
            align: 'center',
            width: 100,
            disabled: true,
          },
          {
            title: '生产锅',
            key: 'kettleId',
            dataIndex: 'kettleId',
            width: 180,
            type: JVXETypes.select,
            align: 'center',
            dictCode: 'tsl_md_kettle,kettle_name,kettle_no',
            disabled: true,
          },
          {
            title: '重量（吨）',
            key: 'formulaQuantity',
            dataIndex: 'formulaQuantity',
            align: 'center',
            width: 120,
          },
          {
            title: '主单位',
            key: 'meUnit',
            dataIndex: 'meUnit',
            dictCode: 'ji_unit',
            type: JVXETypes.select,
            align: 'center',
            width: 100,
            disabled: true,
          },
          //   {
          //     title: '主数量',
          //     key: 'mainQuantity',
          //     dataIndex: 'mainQuantity',
          //     width: 120,
          //   },
          //   {
          //     title: '辅单位',
          //     key: 'auUnit',
          //     dataIndex: 'auUnit',
          //     dictCode: 'zl_unit',
          //     type: JVXETypes.select,
          //     width: 100,
          //     disabled: true,
          //   },
          //   {
          //     title: '辅数量',
          //     key: 'auxiliaryQuantity',
          //     dataIndex: 'auxiliaryQuantity',
          //     width: 120,
          //   },
          {
            title: '计划数量',
            key: 'planQuantity',
            dataIndex: 'planQuantity',
            align: 'center',
            width: 120,
          },
          {
            title: '完成数量',
            key: 'completedQuantity',
            dataIndex: 'completedQuantity',
            align: 'center',
            width: 120,
          },
          {
            title: '剩余数量',
            key: 'residualQuantity',
            dataIndex: 'residualQuantity',
            align: 'center',
            width: 120,
          },
          {
            title: '包装车间',
            key: 'packshopCode_dictText',
            align: 'center',
            width: 120,
            disabled: true,
          },
        ],
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
        style: 'width: 28%;',
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      productType: 0,
      orderCode: '',
      description: '车间生产计划管理页面',
      serviceName: 'tslProWorkshopPlanService',
      activeKey: 'notGenerated',
      notGeneratedList: [],
      planColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '调度单号',
          align: 'center',
          dataIndex: 'scheduleCode',
          sorter: (a, b) => a.scheduleCode.localeCompare(b.scheduleCode, 'en'),
        },
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'orderCode',
          sorter: (a, b) => a.orderCode.localeCompare(b.orderCode, 'en'),
        },
        {
          title: '订单类型',
          align: 'center',
          dataIndex: 'documentType_dictText',
        },
        {
          title: '配方号',
          align: 'center',
          dataIndex: 'formulaNumber',
        },
        {
          title: '数量（吨）',
          align: 'center',
          dataIndex: 'auxiliaryQuantity',
        },
        {
          title: '交货日期',
          align: 'center',
          dataIndex: 'deliveryTime',
        },
      ],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '计划单号',
          align: 'center',
          dataIndex: 'code',
          sorter: (a, b) => a.code.localeCompare(b.code, 'en'),
        },
        {
          title: '生产车间',
          align: 'center',
          dataIndex: 'workshopCode_dictText',
        },
        {
          title: '执行日期',
          align: 'center',
          dataIndex: 'workTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText',
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText',
        },
        {
          title: '制单日期',
          align: 'center',
          dataIndex: 'createTime',
          sorter: (a, b) => a.createTime.localeCompare(b.createTime, 'en'),
        },
        {
          title: '是否变更',
          align: 'center',
          dataIndex: 'isUpdate_dictText',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      approvedTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      tempSaveTable: {
        selectedRowKeys: [],
        selectionRows: [],
        loading: false,
        dataSource: [],
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
      },
      url: {
        waitGenerateList: '/pro/tslProWorkshopPlan/waitGenerateList',
        list: '/pro/tslProWorkshopPlan/list',
        approval: '/pro/tslProWorkshopPlan/approval',
        close: '/pro/tslProWorkshopPlan/close',
        delete: '/pro/tslProWorkshopPlan/delete',
        deleteBatch: '/pro/tslProWorkshopPlan/deleteBatch',
        exportXlsUrl: '/pro/tslProWorkshopPlan/exportXls',
        importExcelUrl: 'pro/tslProWorkshopPlan/importExcel',
        tslProWorkshopPlanDetail: '/pro/tslProWorkshopPlan/queryTslProWorkshopPlanInfoByMainId',
        tradeOrderPackage: '/pro/tslProWorkshopPlan/queryPackageById',
        workshoplist: '/productionexecutionorganization/erpProductionExecutionOrganization',
      },
      dictOptions: {},
      superFieldList: [],
      queryParamApproved: {
        createTime_begin: undefined,
        createTime_end: undefined,
        order: 'create_by desc',
        workshopCode: '',
        salesType: 1,
        status: 999,
      },
      settingColumns: [],
    }
  },
  created() {
    this.queryParamApproved.createTime_begin = getDate('day', -7)
    this.queryParamApproved.createTime_end = getDate('day', 0)
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    rowSelect(record, index) {
      return {
        on: {
          click: (event) => {
            let params = { id: record.id, salesType: '1' }
            this.loadDetailData(params)
          },
        },
      }
    },
    rnoSelect(record, index) {
      return {
        on: {
          click: (event) => {
            if (record.tslProScheduleDetail && record.tslProScheduleDetail.length > 0) {
              this.unMakeTable.dataSource = record.tslProScheduleDetail
              this.loadPackageData({ order: record.orderCode, schedule: record.scheduleCode })
            }
          },
        },
      }
    },
    loadPackageData(params) {
      this.packageTable.dataSource = []
      this.packageTable.loading = true
      getAction(this.url.tradeOrderPackage, params).then((res) => {
        this.packageTable.loading = false
        if (res.success && res.result.length > 0) {
          this.packageTable.dataSource = res.result
        }
      })
    },
    // 暂存中页：处理页码变化
    handleTempSavePageChange(page, pageSize) {
      this.tempSaveTable.ipagination.current = page
      this.tempSaveTable.ipagination.pageSize = pageSize
      this.loadTempSaveData()
    },
    // 暂存中页：处理每页数量变化
    handleTempSavePageSizeChange(page, pageSize) {
      this.tempSaveTable.ipagination.current = page
      this.tempSaveTable.ipagination.pageSize = pageSize
      this.loadTempSaveData()
    },
    // 已审批页：处理页码变化
    handleApprovedPageChange(page, pageSize) {
      this.approvedTable.ipagination.current = page
      this.approvedTable.ipagination.pageSize = pageSize
      this.loadApprovedData()
    },
    // 已审批页：处理每页数量变化
    handleApprovedPageSizeChange(page, pageSize) {
      this.approvedTable.ipagination.current = page
      this.approvedTable.ipagination.pageSize = pageSize
      this.loadApprovedData()
    },
    onWorkshopPlanSelectChangeTempSave(selectedRowKeys, selectedRows) {
      this.tempSaveTable.selectedRowKeys = selectedRowKeys
      // let params = { id: selectedRowKeys[0] }
      // this.loadDetailData(params)
    },
    onWorkshopPlanSelectChangeApproved(selectedRowKeys, selectedRows) {
      this.approvedTable.selectedRowKeys = selectedRowKeys
      // let params = { id: selectedRowKeys[0] }
      // this.loadDetailData(params)
    },
    loadDetailData(params) {
      this.detailTable.dataSource = []
      this.detailTable.loading = true
      getAction(this.url.tslProWorkshopPlanDetail, params).then((res) => {
        this.detailTable.loading = false
        if (res.success && res.result.length > 0) {
          res.result.forEach((element) => {
            element.completedQuantity = element.completedQuantity || 0
            element.residualQuantity = element.planQuantity - element.completedQuantity
          })
          this.detailTable.dataSource = res.result
        }
      })
    },
    addWorkshopPlan(rows) {
      if (!this.selectedRowKeys || this.selectedRowKeys.length == 0) {
        this.$message.error('请至少选择一条生产调度令！')
        return
      }
      let selectedRows = []
      this.dataSource.forEach((element) => {
        if (this.selectedRowKeys.includes(element.id)) {
          selectedRows.push(element)
        }
      })
      this.$refs.modalForm.add(selectedRows, 1)
      this.$refs.modalForm.title = '添加'
      this.$refs.modalForm.disableSubmit = false
    },
    loadNotGeneratedData() {
      this.selectedRowKeys = []
      this.dataSource = []
      this.loading = true
      getAction(this.url.waitGenerateList, { salesType: '1' })
        .then((res) => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
            this.notGeneratedList = deepClone(this.dataSource)
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    loadTempSaveData() {
      this.tempSaveTable.loading = true
      this.queryParam.status = -2
      this.queryParam.order = 'create_by desc'
      this.queryParam.salesType = '1'
      this.queryParam.pageNo = this.tempSaveTable.ipagination.current
      this.queryParam.pageSize = this.tempSaveTable.ipagination.pageSize
      getAction(this.url.list, this.queryParam)
        .then((res) => {
          if (res.success) {
            this.tempSaveTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.tempSaveTable.ipagination.total = res.result.total
            } else {
              this.tempSaveTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.tempSaveTable.loading = false
        })
    },
    loadApprovedData() {
      this.approvedTable.loading = true
      this.queryParamApproved.workshopCode = this.model.workshopCode
      this.queryParam.salesType = '1'
      this.queryParamApproved.pageNo = this.approvedTable.ipagination.current
      this.queryParamApproved.pageSize = this.approvedTable.ipagination.pageSize
      getAction(this.url.list, this.queryParamApproved)
        .then((res) => {
          if (res.success) {
            this.approvedTable.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.approvedTable.ipagination.total = res.result.total
            } else {
              this.approvedTable.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.approvedTable.loading = false
        })
    },
    async OpenOrClose(record) {
      record.isClose = record.isClose == 1 ? 0 : 1
      await postAction(this.url.close, record)
      this.loadApprovedData()
    },
    async handleApproval(record) {
      record.status = record.status == 999 ? -2 : 999
      await postAction(this.url.approval, record)
      if (record.status == 999) {
        this.loadTempSaveData()
      } else {
        this.loadApprovedData()
      }
    },
    async loadData() {
      //分别根据单据状态查询结果并将其放在
      this.queryParam = this.getQueryParams()
      this.handleChangeTab()
    },
    handleChangeTab() {
      this.detailTable.dataSource = []
      this.unMakeTable.dataSource = []
      this.packageTable.dataSource = []
      switch (this.activeKey) {
        case 'notGenerated':
          this.loadNotGeneratedData()
          break
        case 'tempSave':
          this.loadTempSaveData()
          break
        case 'approved':
          this.loadApprovedData()
          console.log('approved')
          break
        default:
          break
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
::v-deep .ant-table-tbody tr {
  height: 36px !important;
}
</style>
